package com.proposition.paper.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.proposition.commons.model.*;
import com.proposition.exam.mapper.ExamAcupointAdultMapper;
import com.proposition.paper.mapper.PaperExamMapper;
import com.proposition.paper.service.PaperExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 试卷导出--常规化
 *
 * @author 杨凯
 * @create 2018-04-16 16:27
 **/
@Service("exportChangGuiHua")
public class PaperExportChangGuiHuaServiceImpl implements PaperExportService {

	@Autowired
	private PaperExamMapper paperExamMapper;

	@Autowired
	private ExamAcupointAdultMapper adultMapper;

	@Override
	public Map<String, Object> getPaperAndAnswerById(String paperId) {
		Map<String,Object> map = new HashMap<>();
		ExamAnswer30 examAnswer30 = null;
		ExamAcupointAdult examAcupointAdult = null;
		ExamAcupointPediatric examAcupointPediatric = null;
		ExamAnswer10 examAnswer10 = null;
		List<ExamAnswer10> answer10List = new ArrayList<ExamAnswer10>();
		PaperExam paperExam = new PaperExam();
		paperExam.setPaperId(paperId);
		//查询试卷对应试题
		paperExam = paperExamMapper.selectOne(paperExam);
		String[] exam1 = paperExam.getExam1().split(",");
		String[] exam2 = paperExam.getExam2().split(",");
		String[] exam3 = paperExam.getExam3().split(",");
		StringBuilder sb = new StringBuilder();
		StringBuilder sb1 = new StringBuilder();
		for (int i = 0; i < exam1.length; i++) {
			String category = exam1[i].substring(0, 2);
			if ("08".equals(category)) {
				examAcupointAdult = adultMapper.selectExamAcupointAdultById(exam1[i]);
				sb.append(examAcupointAdult.getStem())
						.append("、");
				sb1.append("<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>")
						.append("<w:pPr>")
						.append("<w:snapToGrid w:val='0'/>")
						.append("<w:rPr>")
						.append("<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>")
						.append("<w:b/>")
						.append("<w:color w:val='000000'/>")
						.append("<w:sz w:val='28'/>")
						.append("<w:szCs w:val='28'/>")
						.append("</w:rPr>")
						.append("</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:b/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>" + examAcupointAdult.getStem() + "</w:t>" +
								"</w:r>" +
								"</w:p>" +
								"<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
								"<w:pPr>" +
								"<w:snapToGrid w:val='0'/>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>【部位】" + examAcupointAdult.getExamPart().getPart() + "（" + examAcupointAdult.getPartScoreAdult() + "分）</w:t>" +
								"</w:r>" +
								"</w:p>" +
								"<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
								"<w:pPr>" +
								"<w:snapToGrid w:val='0'/>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>【归经】" + examAcupointAdult.getMeridianAdult() + "（" + examAcupointAdult.getMeridianScoreAdult() + "分）</w:t>" +
								"</w:r>" +
								"</w:p>");
				if (examAcupointAdult.getLocation1Adult() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointAdult.getLocation1Adult() + "（" + examAcupointAdult.getLocation1ScoreAdult() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}
				if (examAcupointAdult.getLocation2Adult() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointAdult.getLocation2Adult() + "（" + examAcupointAdult.getLocation2ScoreAdult() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}
				if (examAcupointAdult.getLocation3Adult() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointAdult.getLocation3Adult() + "（" + examAcupointAdult.getLocation3ScoreAdult() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}

			} else if ("07".equals(category)) {
				examAcupointPediatric = new ExamAcupointPediatric().selectOne(new EntityWrapper().eq("id", exam1[i]));
				ExamPart examPart = new ExamPart();
				examPart = examPart.selectById(examAcupointPediatric.getPartIdPediatric());
				sb.append(examAcupointPediatric.getStem())
						.append("、");
				sb1.append("<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>")
						.append("<w:pPr>")
						.append("<w:snapToGrid w:val='0'/>")
						.append("<w:rPr>")
						.append("<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>")
						.append("<w:b/>")
						.append("<w:color w:val='000000'/>")
						.append("<w:sz w:val='28'/>")
						.append("<w:szCs w:val='28'/>")
						.append("</w:rPr>")
						.append("</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:b/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>" + examAcupointPediatric.getStem() + "</w:t>" +
								"</w:r>" +
								"</w:p>" +
								"<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
								"<w:pPr>" +
								"<w:snapToGrid w:val='0'/>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>【部位】" + examPart.getPart() + "（" + examAcupointPediatric.getPartScorePediatric() + "分）</w:t>" +
								"</w:r>" +
								"</w:p>" +
								"<w:p w:rsidR='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
								"<w:pPr>" +
								"<w:snapToGrid w:val='0'/>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"</w:pPr>" +
								"<w:r>" +
								"<w:rPr>" +
								"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
								"<w:sz w:val='28'/>" +
								"<w:szCs w:val='28'/>" +
								"</w:rPr>" +
								"<w:t>【具体部位】" + examAcupointPediatric.getPartDetailPediatric() + "（" + examAcupointPediatric.getPartDetailScorePediatric() + "分）</w:t>" +
								"</w:r>" +
								"</w:p>");
				if (examAcupointPediatric.getLocation1Pediatric() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointPediatric.getLocation1Pediatric() + "（" + examAcupointPediatric.getLocation1ScorePediatric() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}
				if (examAcupointPediatric.getLocation2Pediatric() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointPediatric.getLocation2Pediatric() + "（" + examAcupointPediatric.getLocation2ScorePediatric() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}
				if (examAcupointPediatric.getLocation3Pediatric() != null) {
					sb1.append("<w:p w:rsidR='00C0090D' w:rsidRPr='00C0090D' w:rsidRDefault='00C0090D' w:rsidP='00C0090D'>" +
							"<w:pPr>" +
							"<w:snapToGrid w:val='0'/>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"</w:pPr>" +
							"<w:r>" +
							"<w:rPr>" +
							"<w:rFonts w:ascii='宋体' w:hAnsi='宋体' w:hint='eastAsia'/>" +
							"<w:sz w:val='28'/>" +
							"<w:szCs w:val='28'/>" +
							"</w:rPr>" +
							"<w:t>【定位】" + examAcupointPediatric.getLocation3Pediatric() + "（" + examAcupointPediatric.getLocation3ScorePediatric() + "分）</w:t>" +
							"</w:r>" +
							"</w:p>");
				}
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("。");
		map.put("exam1", sb.toString());
		map.put("exam1Answer", sb1.toString());
		//清空StringBuilder
		sb.setLength(0);
		for (int i = 0; i < exam2.length; i++) {
			examAnswer10 = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", exam2[i]));
			answer10List.add(examAnswer10);
			sb.append(i + 1)
					.append(".")
					.append(examAnswer10.getStem())
					.append("。")
					.append("（"+examAnswer10.getTotalScore()+"分）")
					.append("<w:br/>");
		}
		map.put("exam2list", answer10List);
		map.put("exam2", sb.substring(0, sb.length() - 7));
		examAnswer30 = new ExamAnswer30().selectOne(new EntityWrapper().eq("id", exam3[0]));
		map.put("exam3", examAnswer30);
		return map;
	}
}
